﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using AvaloniaApplication1.Configs;
using AvaloniaApplication1.Helper;
using AvaloniaApplication1.Models;

namespace AvaloniaApplication1.Views
{
    public class MainWindow : Window
    {
        ComboBox cmbWindowName;

        List<Type> lisTypes = new List<Type>();

        public MainWindow()
        {
            InitializeComponent();
#if DEBUG

           
            Assembly assembly = Assembly.GetAssembly(this.GetType());
            
            Type[] types = assembly.GetTypes();
            
            List<string> lisTypeNames = new List<string>();
            Type baseWindowType = typeof(Window);
            foreach (Type type in types)
            {
                if (baseWindowType.IsAssignableFrom(type))
                {
                    lisTypes.Add(type);
                    lisTypeNames.Add(type.Name);
                }
            }

          

             cmbWindowName = this.FindControl<ComboBox>(nameof(cmbWindowName));
            cmbWindowName.DataContext = lisTypeNames;
            cmbWindowName.Items = lisTypeNames;
            if (ProjectConfig.Instance!=null)
            {
                if (lisTypeNames.Count - 1 >= ProjectConfig.Instance.LastSelectIndex)
                {
                    cmbWindowName.SelectedIndex = ProjectConfig.Instance.LastSelectIndex;
                }
            }
           

            //Type windowType = Type.GetType($"AvaloniaApplication1.Views.{WindowName.Text}");
            //Window window = Activator.CreateInstance(windowType) as Window;
            //window.Show();
            //this.AttachDevTools();
            //CorneradisWindow signCertificateWindow = new CorneradisWindow();
            //signCertificateWindow.Show();
            //Button button =new Button();
            //button.Click+=new EventHandler<RoutedEventArgs>();


            //Assembly assembly= Assembly.GetAssembly(typeof(Thickness));
            //Type[] types= assembly.GetTypes();
            //List<string> lisVaalues = new List<string>();
            //foreach (Type type in types)
            //{
            //    lisVaalues.Add(type.Name);
            //}
            //string projectConfig = XmlHelp.ToString(lisVaalues, lisVaalues.GetType(), Encoding.Default);
            //XDocument xDocument = XDocument.Parse(projectConfig);
            //xDocument.Save($"{AppDomain.CurrentDomain.BaseDirectory}123.xml");



#endif
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
            TestReactiveUserControl<Student> test = new TestReactiveUserControl<Student>();
            Student student= test.GetViewModel();
        }

        private void btnShowSignWindow_Click(object sender, RoutedEventArgs e)
        {
            SignCertificateWindow signCertificateWindow=new SignCertificateWindow();
            signCertificateWindow.Show();
            //this.PointerPressed += MainWindow_PointerPressed;
        }

        private void MainWindow_PointerPressed(object sender, Avalonia.Input.PointerPressedEventArgs e)
        {

            ShowTreeViewWindow testTree = new ShowTreeViewWindow();
            testTree.Show();
        }


        private void TestDateTemp_PointerPressed(object sender, Avalonia.Input.PointerPressedEventArgs e)
        {

            TestTreeViewWindow testTree = new TestTreeViewWindow();
            testTree.Show();
        }

        private void Button_OnClick(object? sender, RoutedEventArgs e)
        {
            TestGridSplitWindow testGridSplitWindow = new TestGridSplitWindow();
            testGridSplitWindow.Show();
            SystemIcons.Application.ToBitmap();

        }

        private void ButtonTestRoutedEventArgs_OnClick(object? sender, RoutedEventArgs e)
        {
            this.Title += "1";
        }

        private void CommonClickHandler(object sender, RoutedEventArgs e)
        {
            var source = e.Source as Control;
            switch (source.Name)
            {
                case "YesButton":
                    // do something here ...
                    break;
                case "NoButton":
                    // do something ...
                    break;
                case "CancelButton":
                    // do something ...
                    break;
            }
            e.Handled = true;
        }

        private void BtnShowWindow_OnClick(object? sender, RoutedEventArgs e)
        {
            ProjectConfig projectConfig = ProjectConfig.Instance;
            projectConfig.LastSelectIndex = cmbWindowName.SelectedIndex;
            projectConfig.Save();
            Window window = Activator.CreateInstance(lisTypes[cmbWindowName.SelectedIndex]) as Window;
            window.Show(this);
            
        }

        private void BtnSave_OnClick(object? sender, RoutedEventArgs e)
        {
            ProjectConfig projectConfig = new ProjectConfig();
            projectConfig.Save();
        }
    }
}
